package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.dto.SelectStudentByIdDTO;
import com.itheima.tlias84.dto.SelectStudentDTO;
import com.itheima.tlias84.mapper.StudentMapper;
import com.itheima.tlias84.po.InsterStudentParam;
import com.itheima.tlias84.po.SelectStudentPageParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;
    @Override
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectStudentDTO> dtos = studentMapper.selectStudentByPage(param);
        PageVO<SelectStudentDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }

    @Override
    public int insertStudent(InsterStudentParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        int i = studentMapper.insertStudent(param);
        return i;
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        int i = studentMapper.batchDelete(ids);
        return i;
    }

    @Override
    public SelectStudentByIdDTO selectStudentById(Integer id) {
        SelectStudentByIdDTO dto = studentMapper.selectStudentById(id);
        return dto;
    }

    @Override
    public int updateStudent(UpdateStudentParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        int i = studentMapper.updateStudent(param);
        return i;
    }

    @Override
    public int violation(Integer id) {
        return studentMapper.violation(id);
    }
}
